<template>
  <Teleport to="body">
    <Transition name="dialog-fade">
      <div v-if="visible" class="confirm-overlay" @click="handleCancel">
        <Transition name="dialog-zoom">
          <div v-if="visible" class="confirm-dialog" @click.stop>
            <div class="confirm-header">
              <span class="confirm-icon" :class="iconClass">{{ iconText }}</span>
              <h3 class="confirm-title">{{ title }}</h3>
            </div>
            <div class="confirm-body">
              <p class="confirm-message">{{ message }}</p>
            </div>
            <div class="confirm-footer">
              <button class="confirm-btn confirm-btn-cancel" @click="handleCancel">
                {{ cancelText }}
              </button>
              <button class="confirm-btn confirm-btn-confirm" @click="handleConfirm">
                {{ confirmText }}
              </button>
            </div>
          </div>
        </Transition>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '提示'
  },
  message: {
    type: String,
    default: ''
  },
  type: {
    type: String,
    default: 'warning', // 'warning', 'danger', 'info'
    validator: (value) => ['warning', 'danger', 'info'].includes(value)
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  cancelText: {
    type: String,
    default: '取消'
  }
})

const emit = defineEmits(['confirm', 'cancel', 'update:visible'])

// 根据类型显示不同的图标
const iconClass = computed(() => {
  return `confirm-icon-${props.type}`
})

const iconText = computed(() => {
  const icons = {
    warning: '⚠️',
    danger: '🗑️',
    info: 'ℹ️'
  }
  return icons[props.type] || icons.warning
})

const handleConfirm = () => {
  emit('confirm')
  emit('update:visible', false)
}

const handleCancel = () => {
  emit('cancel')
  emit('update:visible', false)
}
</script>

<style scoped>
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.confirm-dialog {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 420px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.confirm-header {
  padding: 24px 24px 16px;
  text-align: center;
}

.confirm-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 12px;
}

.confirm-icon-warning {
  filter: drop-shadow(0 2px 8px rgba(250, 173, 20, 0.3));
}

.confirm-icon-danger {
  filter: drop-shadow(0 2px 8px rgba(255, 77, 79, 0.3));
}

.confirm-icon-info {
  filter: drop-shadow(0 2px 8px rgba(24, 144, 255, 0.3));
}

.confirm-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.confirm-body {
  padding: 0 24px 24px;
  text-align: center;
}

.confirm-message {
  margin: 0;
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.confirm-footer {
  padding: 16px 24px;
  background: #fafafa;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.confirm-btn {
  padding: 8px 20px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
  font-weight: 500;
}

.confirm-btn-cancel {
  background: white;
  border: 1px solid #d9d9d9;
  color: #333;
}

.confirm-btn-cancel:hover {
  color: #1890ff;
  border-color: #1890ff;
}

.confirm-btn-confirm {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.confirm-btn-confirm:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

/* 动画效果 */
.dialog-fade-enter-active,
.dialog-fade-leave-active {
  transition: opacity 0.3s ease;
}

.dialog-fade-enter-from,
.dialog-fade-leave-to {
  opacity: 0;
}

.dialog-zoom-enter-active,
.dialog-zoom-leave-active {
  transition: all 0.3s ease;
}

.dialog-zoom-enter-from {
  opacity: 0;
  transform: scale(0.8) translateY(-20px);
}

.dialog-zoom-leave-to {
  opacity: 0;
  transform: scale(0.9);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .confirm-dialog {
    max-width: 90%;
  }

  .confirm-footer {
    flex-direction: column;
  }

  .confirm-btn {
    width: 100%;
  }
}
</style>
